//
//  ContactBook.m
//  GolfFriend
//
//  Created by Vescky on 14-2-24.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "ContactBook.h"
#define TB_CONTACTBOOK @"tb_contact_book"

@implementation ContactBook

ContactBook *defaultContactBook;
//-- id
//-- uId userId
//-- nickName
//-- prestige 威望，积分
//-- sex male/female
//-- avatar 头像link
//-- motto 座右铭
//-- area eg.广东.广州
//-- teams json for teamId
//本地数据,local
//## relationShip:friend,black
+ (id)shareContactBook {
    if (!defaultContactBook) {
        defaultContactBook = [[ContactBook alloc] init];
        DB *dbase = [[DB alloc] init];
        FMDatabase *database = [dbase getDatabase];
        [database executeUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(recordId integer primary key,uId text,nickName text,prestige text,sex text,avatar text,motto text,area text,teams text,teamName text,teamLogo text,relationShip text)",TB_CONTACTBOOK]];
        [database close];
    }
    return defaultContactBook;
}


//更新所有
- (bool)refreshFriendList:(NSMutableArray*)_arr {
    if (!_arr || [_arr count] < 1) {
        return NO;
    }
    for (int i = 0; i < [_arr count]; i++) {
        [self saveFriend:[_arr objectAtIndex:i]];
    }
    return YES;
}

//保存，更新
- (bool)saveFriend:(NSDictionary*)_dict {
    if (!_dict || [_dict count] < 1) {
        return NO;
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:_dict];
    [params setValue:@"friend" forKey:@"relationShip"];
    params = [self filterParams:params];//过滤多余的字段
    NSEnumerator *pEm = [params keyEnumerator];
    for (NSString *key in pEm) {
        if (![params objectForKey:key]) {
            [params removeObjectForKey:key];
        }
    }
    if ([self isFriendExist:[_dict objectForKey:@"uId"]]) {
         [self update:params where:params inTable:TB_CONTACTBOOK];
    }
    else {
        //无记录，插入
        [self insert:params inTable:TB_CONTACTBOOK];
    }
    return YES;
}

- (bool)saveFriendList:(NSMutableArray*)arr {
    if (!arr || [arr count] < 1) {
        return NO;
    }
    for (int i = 0; i < [arr count]; i++) {
        [self saveFriend:[arr objectAtIndex:i]];
    }
    return YES;
}

- (bool)saveBlack:(NSDictionary*)_dict {
    if (!_dict || [_dict count] < 1) {
        return NO;
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:_dict];
    [params setValue:@"black" forKey:@"relationShip"];
    params = [self filterParams:params];//过滤多余的字段
    NSEnumerator *pEm = [params keyEnumerator];
    for (NSString *key in pEm) {
        if (![params objectForKey:key]) {
            [params removeObjectForKey:key];
        }
    }
    if ([self isFriendExist:[_dict objectForKey:@"uId"]]) {
        [self update:params where:params inTable:TB_CONTACTBOOK];
    }
    else {
        //无记录，插入
        [self insert:params inTable:TB_CONTACTBOOK];
    }
    return YES;
}

- (bool)saveBlackList:(NSMutableArray*)arr {
    if (!arr || [arr count] < 1) {
        return NO;
    }
    for (int i = 0; i < [arr count]; i++) {
        [self saveBlack:[arr objectAtIndex:i]];
    }
    return YES;
}

- (bool)saveVisitor:(NSDictionary*)_dict {
    if (!_dict || [_dict count] < 1) {
        return NO;
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:_dict];
    [params setValue:@"visitor" forKey:@"relationShip"];
    params = [self filterParams:params];//过滤多余的字段
    NSEnumerator *pEm = [params keyEnumerator];
    for (NSString *key in pEm) {
        if (![params objectForKey:key]) {
            [params removeObjectForKey:key];
        }
    }
    if ([self isFriendExist:[_dict objectForKey:@"uId"]]) {
        [self update:params where:params inTable:TB_CONTACTBOOK];
    }
    else {
        //无记录，插入
        [self insert:params inTable:TB_CONTACTBOOK];
    }
    return YES;
}

- (bool)saveVisitorList:(NSMutableArray*)arr {
    if (!arr || [arr count] < 1) {
        return NO;
    }
    for (int i = 0; i < [arr count]; i++) {
        [self saveVisitor:[arr objectAtIndex:i]];
    }
    return YES;
}

- (NSMutableArray*)getFriendList {
    return [self queryAll:TB_CONTACTBOOK];
}

- (NSMutableDictionary*)getUserInfo:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    NSString *qConfition = [NSString stringWithFormat:@"select * from %@ where uId = '%@' limit 1",TB_CONTACTBOOK,_uId];
    NSMutableArray *arr = [self queryByConfition:qConfition];
    if (isValueSet(arr)) {
        return [arr objectAtIndex:0];
    }
    return nil;
}

- (bool)changeToBlackList:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    if ([self isFriendExist:_uId]) {
        [self update:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"black",@"relationShip", nil]
               where:[[NSMutableDictionary alloc] initWithObjectsAndKeys:_uId,@"uId", nil] inTable:TB_CONTACTBOOK];
        return YES;
    }
    return NO;
}

- (bool)changeToFriendList:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    if ([self isFriendExist:_uId]) {
        [self update:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"friend",@"relationShip", nil]
               where:[[NSMutableDictionary alloc] initWithObjectsAndKeys:_uId,@"uId", nil] inTable:TB_CONTACTBOOK];
        return YES;
    }
    return NO;
}

- (bool)deleteFriend:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    if ([self isFriendExist:_uId]) {
        [self remove:@"uId" is:_uId inTable:TB_CONTACTBOOK];
        return YES;
    }
    return NO;
}

- (bool)isFriendExist:(NSString*)_uId {
    if (!_uId || _uId.length < 1) {
        return NO;
    }
    NSString *qConfition = [NSString stringWithFormat:@"select * from %@ where uId = '%@' limit 1",TB_CONTACTBOOK,_uId];
    NSMutableArray *arr = [self queryByConfition:qConfition];
    if (arr && [arr count] > 0) {
        return YES;
    }
    return NO;
}

- (NSMutableDictionary*)filterParams:(NSMutableDictionary*)_dict {
    NSDictionary *tbDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"-",@"recordId",
                            @"-",@"uId",
                            @"-",@"nickName",
                            @"-",@"prestige",
                            @"-",@"sex",
                            @"-",@"avatar",
                            @"-",@"motto",
                            @"-",@"area",
                            @"-",@"teams",
                            @"-",@"teamName",
                            @"-",@"teamLogo",
                            @"-",@"relationShip",nil];
    NSMutableDictionary *resultDict = [[NSMutableDictionary alloc] init];
    NSEnumerator *enm = [_dict keyEnumerator];
    for (NSString *key in enm) {
        if ([tbDict objectForKey:key]) {
            [resultDict setObject:[_dict objectForKey:key] forKey:key];
        }
    }
    return resultDict;
}

@end
